﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using System.Data;
using System.Text;

public partial class Evaluate_BaseInputs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (DepartmentID.Substring(0, 4) != "1300")
        {
            Response.Redirect("BaseInputsDatachment.aspx");
        }
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "320201"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
        }
        if (!IsPostBack)
        {
            BindYearList();
            BindGrid();
        }
    }
    private void BindYearList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            YearList.Items.Add(li);
        }
        YearList.SelectedValue = DateTime.Now.Year.ToString();
    }
    protected void YearList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid();
    }

    private string GetWhere()
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string ReturnStr = string.Empty;

        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            ReturnStr = "And SubString(a.DepartmentID,6,1) IN('1','2','3','4') And  SubString(a.DepartmentID,5,1) ='0'";
        }
        else
        {
            //总队人员
            if (DepartmentID.Substring(2, 3).Equals("000"))
            {
                ReturnStr = " And SubString(a.DepartmentID,6,1)='" + DepartmentID.Substring(5, 1) + "' And SubString(a.DepartmentID,5,1)='0' ";
            }
            else
            {
                //如果属于支队司政后防
                if (DepartmentID.Substring(4, 1).Equals("0"))
                {
                    ReturnStr = " And Left(a.DepartmentID,6)= '" + DepartmentID.Substring(0, 6) + "'  And SubString(a.DepartmentID,5,1)='0'";
                }

            }
        }

        return ReturnStr;

    }

    private int GetGroupByLen()
    {
        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        //是否是司政后防管理员
        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return 4;
        }
        else
        {
            return 6;
        }
    }

    private void BindGrid()
    {
        if (GetWhere() == string.Empty)
        {
            return;
        }
        string year = YearList.SelectedValue;
        UserGrid.Caption = "<span>" + year + "年基础数据成绩录入</span>";
        StringBuilder sql = new StringBuilder();
        sql.Append("select " + year + "as yearvalue, Left(a.departmentid," + GetGroupByLen() + ") as departmentid ");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=1 then itemscore end) as 一月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=2 then itemscore end) as 二月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=3 then itemscore end) as 三月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=4 then itemscore end) as 四月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=5 then itemscore end) as 五月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=6 then itemscore end) as 六月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=7 then itemscore end) as 七月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=8 then itemscore end) as 八月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=9 then itemscore end) as 九月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=10 then itemscore end) as 十月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=11 then itemscore end) as 十一月");
        sql.Append(", sum(case when b.yearvalue=" + year + " and b.monthvalue=12 then itemscore end) as 十二月");
        sql.Append(", sum(case when b.yearvalue=" + year + " then itemscore end) as 合计");
        sql.Append(" from account_departments a left join baseitems b on a.departmentid=b.departmentid");
        sql.Append(" where substring(a.departmentid,3,2)<>'00' " + GetWhere() + " group by Left(a.departmentid," + GetGroupByLen() + ") order by  Left(a.departmentid," + GetGroupByLen() + ")");
        UserGrid.DataSource = SplitPage.ExecuteSQL(sql.ToString());
        UserGrid.DataBind();
    }
    // b.BaseID as BaseID
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    public string GetDepartmentName(string departmentID)
    {
        // return departmentID;
        return Department.GetDepartment(departmentID.Substring(0, 4) + "000").AliasName;
    }
    public string GetScore(string score)
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        decimal total = Yipusoft.Evaluate.BLL.Base.GetList("departmentId like '" + DepartmentID.Substring(0, 6) + "%' and Flag=0 ").Sum(o => o.TotalScore);
        if (score == "")
            return total.ToString();
        else
            return (total -Convert.ToInt32(score)).ToString();
    }

    public string GetScoreAll(string score)
    {
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        decimal total = Yipusoft.Evaluate.BLL.Base.GetList("departmentId like '" + DepartmentID.Substring(0, 6) + "%' and Flag=0 ").Sum(o => o.TotalScore) * 12;
        if (score == "")
            return total.ToString();
        else
            return (total - Convert.ToInt32(score)).ToString();
    }
}
